package com.q.tqb.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.JsonElement;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.q.tqb.R;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse2;
import com.q.tqb.util.http.OkHttpUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class JoinAgentActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.rb_1)
    RadioButton rb1;
    @BindView(R.id.rb_2)
    RadioButton rb2;
    @BindView(R.id.rb_3)
    RadioButton rb3;
    @BindView(R.id.tv_city)
    public TextView tv_city;

    //申明对象
    CityPickerView mPicker = new CityPickerView();
    public String cityCode;
    public String type;

    @Override
    public int getLayoutId() {
        return R.layout.activity_joinagent;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("申请加入代理");
        /**
         * 预先加载仿iOS滚轮实现的全部数据
         */
        mPicker.init(this);
        rb1.setOnCheckedChangeListener(this);
        rb2.setOnCheckedChangeListener(this);
        rb3.setOnCheckedChangeListener(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_city, R.id.regist_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_city:
                selectCity();
                break;
            case R.id.regist_btn:
                doJoin();
                break;
        }
    }

    private void selectCity() {
        //添加默认的配置，不需要自己定义
        CityConfig cityConfig = new CityConfig.Builder().build();
        cityConfig.setDefaultProvinceName("北京");
        cityConfig.setDefaultCityName("北京");
        cityConfig.setDefaultDistrict("西城区");
        cityConfig.setProvinceCyclic(false);//省份滚轮是否可以循环滚动
        cityConfig.setCityCyclic(false);//城市滚轮是否可以循环滚动
        cityConfig .setDistrictCyclic(false);//区县滚轮是否循环滚动
        cityConfig.setShowGAT(true);
        if (rb1.isChecked()) {
            cityConfig.mWheelType = CityConfig.WheelType.PRO;
        } else if (rb2.isChecked()) {
            cityConfig.mWheelType = CityConfig.WheelType.PRO_CITY;
        } else if (rb3.isChecked()) {
            cityConfig.mWheelType = CityConfig.WheelType.PRO_CITY_DIS;
        }
        mPicker.setConfig(cityConfig);

        //监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                //省份
                if (province != null && city != null && district != null) {
                    tv_city.setText(province.getName() + city.getName() + district.getName());
                    if (rb1.isChecked()) {
                        cityCode = province.getId();
                    } else if (rb2.isChecked()) {
                        cityCode = city.getId();
                    } else if (rb3.isChecked()) {
                        cityCode = district.getId();
                    }
                }
            }

            @Override
            public void onCancel() {

            }
        });

        //显示
        mPicker.showCityPicker();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        tv_city.setText("");
        cityCode = "";
    }

    private void doJoin() {
        if (rb1.isChecked()) {
            type = "3";
        } else if (rb2.isChecked()) {
            type = "2";
        } else if (rb3.isChecked()) {
            type = "1";
        }
        int uid = (int) SPUtil.get(this, "uid", 0);
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        map.put("type", type);
        map.put("cityid", cityCode);
        mProgressDialog.show();
        OkHttpUtil.getInstance().post2(this, AppConst.URL_ADD_AGENT, map, new IResponse2() {

            @Override
            public void onSuccess(JsonElement element, String msg) {
                showToast(msg);
                mProgressDialog.dismiss();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }


}
